<template>
  <div class="app-container">
    <!-- 搜索框 -->
    <div class="search">
      <el-form ref="search" :inline="true" :model="search">
        <!-- 设备编号 -->
        <el-form-item label="订单编号:" size="medium">
          <el-input
            v-model="search.orderNo"
            clearable
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="选择日期:" size="medium">
          <el-date-picker
            v-model="chooseDate"
            type="daterange"
            range-separator="~"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-button
          type="primary"
          class="my-primary1"
          icon="el-icon-search"
          @click="getOrderList"
        >查询</el-button>
      </el-form>
    </div>

    <!-- 表单区域 -->
    <div class="result">
      <!-- 头部 -->
      <!-- 表单主体 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          type="index"
          :index="(index) => (search.pageIndex - 1) * 10 + (index + 1)"
          label="序号"
          width="80"
        />
        <el-table-column prop="orderNo" label="订单编号" />
        <el-table-column prop="skuName" label="商品名称" />
        <el-table-column prop="price" label="订单金额(元)" />
        <el-table-column prop="innerCode" label="设备编号" />
        <el-table-column prop="status" label="订单状态" />
        <el-table-column prop="createTime" label="订单日期" />
        <el-table-column label="操作" width="100">
          <template v-slot="{ row }">
            <el-button
              type="text"
              class="el-button--primary-text"
              @click.native="showDetailDialog(row)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表单底部 -->
      <div class="pagination-container">
        <el-pagination
          layout="slot,prev,next"
          :current-page.sync="search.pageIndex"
          prev-text="上一页"
          next-text="下一页"
          background
          hide-on-single-page
          :total="totalCount"
          :page-size="search.pageSize"
          @current-change="getOrderList"
        >
          <template order="-1">
            <span
              class="el-pagination__total"
            >共{{ totalCount }}条记录 第 {{ search.pageIndex }}/{{
              totalPage
            }}
              页</span>
          </template>
        </el-pagination>
      </div>
    </div>
    <!-- 新增弹窗 -->
    <!-- 查看详情 -->
    <el-dialog title="订单详情" :visible.sync="detailDialog" width="630px">
      <div class="task-status">
        <img
          class="icon"
          src=""
          alt=""
        >
        <span class="status">{{ orderList.status }}</span>
        <img
          src="https://likede2-admin.itheima.net/img/pic_2.3b5af41c.png"
          alt=""
          class="pic"
        >
      </div>

      <el-form class="taskDetail">
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单编号：" label-width="110px">
              {{ orderList.orderNo }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品名称：" label-width="110px">
              {{ orderList.skuName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单金额：" label-width="110px">
              {{ orderList.price }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备编号：" label-width="110px">
              {{ orderList.innerCode }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间：" label-width="110px">
              {{ orderList.createTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="完成时间：" label-width="110px">
              {{ orderList.updateTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付方式：" label-width="110px">
              {{ orderList.payType }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备地址：" label-width="110px">
              {{ orderList.addr }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  getOrderList
} from '@/api/vm'
import moment from 'moment'
import store from '@/store'
export default {
  name: 'OrderContainer',
  data() {
    return {
      search: {
        pageIndex: 1,
        pageSize: 10,
        orderNo: null,
        startDate: '',
        endDate: ''
      },
      chooseDate: [],
      repairCount: 0,
      supplyCount: 0,
      orderAmount: 0,
      orderCount: 0,
      orderList: {},
      skuCollectList: [],
      createUserId: store.getters.userId,
      partnerId: null,
      addFormDialog: false,
      detailDialog: false,
      today: moment().format('YYYY-MM-DD'), // 今天日期
      firstDayMonth: moment().startOf('month').format('YYYY-MM-DD'), // 本月第一天日期
      tableData: [],
      totalPage: 0,
      totalCount: 0,
      currentName: '',
      currentId: null,
      rules: {
        policyName: [{ required: true, message: '请输入', trigger: 'change' }],
        discount: [{ required: true, message: '请输入', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 查看详情
    async showDetailDialog(row) {
      console.log(row)
      this.orderList = row
      this.detailDialog = true
    },
    // 查找
    async getOrderList() {
      this.chooseDate = this.chooseDate.map(item => {
        item = moment(item).format('YYYY-MM-DD')
        return item
      })
      console.log(this.chooseDate)
      this.search.startDate = this.chooseDate[0]
      this.search.endDate = this.chooseDate[1]
      console.log(this.search)

      const res = await getOrderList({ ...this.search })
      console.log(res)
      this.tableData = res.currentPageRecords.map(item => {
        item.price = (item.price / 100).toFixed(2)
        item.status = item.status === 0 ? '创建' : item.status === 1 ? '支付完成'
          : item.status === 2 ? '出货成功' : item.status === 3 ? '出货失败' : ''
        item.payType = item.payType === '1' ? '支付宝支付' : item.payType === '2' ? '微信支付' : '其他支付'
        item.createTime = moment(item.createTime).format('YYYY.MM.DD HH:mm:ss')
        item.updateTime = moment(item.updateTime).format('YYYY.MM.DD HH:mm:ss')
        item.addr = item.addr.split('-').at(-1)
        return item
      })
      this.totalCount = +res.totalCount
      this.totalPage = +res.totalPage
    },
    // 新增
    // 删除
    // 关闭弹窗(清空表单)
    closeFn() {
      this.currentId = null
      this.addForm = {
        policyName: '',
        discount: null
      }
      this.$refs.addForm.resetFields()
      this.getOrderList()
      this.addFormDialog = false
    }
    // 修改
    // 确认
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/common.scss";
.search .el-form-item {
  margin-bottom: 0;
}

::v-deep .el-form-item__label {
  font-weight: normal;
}

.taskDetail .el-form-item {
  margin-bottom: 0;
}

</style>
